﻿
Imports System.Data.SqlClient

Public Class FrmAreas
    Dim classController As New Class_RC_Controller.rc_Mantenimiento

    Private _codArea As Integer = 0

    Public Property codArea() As Integer
        Get
            Return _codArea
        End Get
        Set(ByVal value As Integer)
            _codArea = value
        End Set
    End Property


    Private Sub FrmAreas_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.CargarDatos()

    End Sub
   
    Public Sub limpiar()
        Me.txtcodigo.Text = ""
        Me.txtdescripcion.Text = ""
        Me.txtobsarea.Text = ""
    End Sub
    Private Sub CargarDatos()
        Try
            If codArea > 0 Then

                Dim ds As New DataTable
                ds = classController.get_Area_All(oiml.dbConn, codArea, "")

                If Not ds Is Nothing Then
                    txtcodigo.Text = ds.Rows(0)("codArea")
                    txtdescripcion.Text = ds.Rows(0)("descArea")
                    txtobsarea.Text = ds.Rows(0)("ObsArea")
                    btneliminar.Enabled = True
                End If
            Else
                btneliminar.Enabled = False
                txtdescripcion.Focus()


            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try
    End Sub
    Private Function ValidarCampos() As Boolean

        Dim controlCampos As Boolean = False

        If txtDescripcion.Text = "" Then
            ErrorProvider.SetError(txtDescripcion, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider.SetError(txtDescripcion, "")
        End If        '

        Return controlCampos

    End Function
    Private Sub btnguardar_Click(sender As System.Object, e As System.EventArgs) Handles btnguardar.Click
        If ValidarCampos() Then
            Exit Sub
        End If

        If codArea = 0 Then
            codArea = classController.Insert_Areas(oiml.dbConn, txtdescripcion.Text, txtobsarea.Text)
            If codArea > 0 Then
                MessageBox.Show("Registro grabado con exito!", "Areas", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Me.DialogResult = Windows.Forms.DialogResult.OK
                Me.Close()

            Else
                MessageBox.Show("Error: " & classController.MessageError.Message, "Areas", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        Else
            If classController.Update_Area(oiml.dbConn, codArea, txtdescripcion.Text, txtobsarea.Text) Then
                MessageBox.Show("Registro grabado con exito!", "Areas", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Me.DialogResult = Windows.Forms.DialogResult.OK
                Me.Close()

            Else
                MessageBox.Show("Error: " & classController.MessageError.Message, "Areas", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        End If
        

    End Sub

    

    Private Sub btncancelar_Click(sender As System.Object, e As System.EventArgs) Handles btncancelar.Click
        Me.Close()
    End Sub



    Private Sub btneliminar_Click(sender As System.Object, e As System.EventArgs) Handles btneliminar.Click
        If codArea > 0 Then
            If MessageBox.Show("Desea eliminar el registro seleccionado?", "Areas", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                If classController.Delete_Area(oiml.dbConn, codArea) Then
                    Me.DialogResult = Windows.Forms.DialogResult.OK
                    Me.Close()

                Else
                    MessageBox.Show("Error: " & classController.MessageError.Message, "Area", MessageBoxButtons.OK, MessageBoxIcon.Error)

                End If

            End If

        End If
    End Sub


End Class